package com.example.meet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.framework.helper.GlideHelper;
import com.example.meet.R;
import com.example.meet.model.AddFriendModel;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author:0.2℃
 * @date： 2022-04-05 15:55
 * @Description ：多类型的布局
 */
public class AddFriendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    // 标题
    public static final int TYPE_TITLE = 0;
    // 内容
    public static final int TYPE_CONTENT = 1;

    private Context mContext;
    private List<AddFriendModel> mData;
    private LayoutInflater inflater;

    private OnClickListener listener;

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public AddFriendAdapter(Context mContext, List<AddFriendModel> mData) {
        this.mContext = mContext;
        this.mData = mData;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            return new TitleViewHolder(inflater.inflate(R.layout.layout_search_title_item, null));
        } else if (viewType == TYPE_CONTENT){
            return new ContentViewHolder(inflater.inflate(R.layout.layout_search_user_item, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AddFriendModel model = mData.get(position);

        if (model.getType() == TYPE_TITLE) {
            TitleViewHolder viewHolder = ((TitleViewHolder)holder);
            viewHolder.tv_title.setText(model.getTitle());
        } else if (model.getType() == TYPE_CONTENT) {
            ContentViewHolder viewHolder = (ContentViewHolder)holder;
            // 设置头像
            GlideHelper.loadUrl(mContext, model.getPhoto(), viewHolder.iv_photo);
            // 设置性别
            viewHolder.iv_sex.setImageResource(model.isSex() ? R.drawable.img_boy_icon : R.drawable.img_girl_icon);
            viewHolder.tv_age.setText(model.getAge() + "岁");
            viewHolder.tv_nickname.setText(model.getNickname());
            viewHolder.tv_desc.setText(model.getDesc());
        }

        /**
         * 点击到对应的position之后，把点击的监听传出去
         */
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getType();
    }

    class TitleViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title;
        public TitleViewHolder(View itemView) {
            super(itemView);

            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }

    class ContentViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView iv_photo;
        private ImageView iv_sex;
        private TextView tv_nickname;
        private TextView tv_age;
        private TextView tv_desc;
        public ContentViewHolder(View itemView) {
            super(itemView);

            iv_photo = itemView.findViewById(R.id.iv_photo);
            iv_sex = itemView.findViewById(R.id.iv_sex);
            tv_nickname = itemView.findViewById(R.id.tv_nickname);
            tv_age = itemView.findViewById(R.id.tv_age);
            tv_desc = itemView.findViewById(R.id.tv_desc);
        }
    }

    public interface OnClickListener {
        void onClick(int position);
    }
}
